﻿@using Kobets.Automation.Chimera.Orchestrator.Models

<script type="text/javascript">
    $(window).resize(function () {
        ResizeGrid('#AgentsGrid');
    });
    
    $(window).load(function () {
        var firstLoad = true;
        ResizeGrid('#AgentsGrid');
    });

    function error_handler(e) {
        if (e.errors) {
            var message = "Errors:\n";
            $.each(e.errors, function (key, value) {
                if ('errors' in value) {
                    $.each(value.errors, function () {
                        message += this + "\n";
                    });
                }
            });
            alert(message);
        }
    }
</script>

<style scoped>
    input {
        width: auto !important;
    }
</style>

<div>
    @(Html.Kendo().Grid<AgentsModel>()
           .Name("AgentsGrid")
           .Columns(columns =>
           {
               columns.Bound(p => p.Name);
               columns.Bound(p => p.Grp).Title("Group");
               columns.Bound(p => p.Attributes);
               columns.Command(command => { command.Edit(); command.Destroy(); });
           })
           .ToolBar(toolbar => toolbar.Create())
           .Editable(editable => editable.Mode(GridEditMode.InLine))
           .Sortable()
           .Pageable()
           .Scrollable()
           .DataSource(dataSource => dataSource
                .Ajax()
                .ServerOperation(false)
                .Sort(s => s.Add(c => c.Name))
                .Events(events => events.Error("error_handler"))
                .Model(model => {
                    model.Id(p => p.Name);
                    model.Field(p => p.Name);
                    model.Field(p => p.Grp);
                    model.Field(p => p.Attributes);
                })
                .Read(read => read.Action("GetData", "Resource"))
                .Create(create => create.Action("AddData", "Resource"))
                .Update(update => update.Action("EditData", "Resource"))
                .Destroy(destroy => destroy.Action("DeleteData", "Resource"))
                .PageSize(20)
           )
    )
</div>